<?php
/**
 * Auth.php		user auth
 *
 * @package		
 * @Copyright	(c) All rights reserved
 * @Author		skyCrack <skyCrack@126.com>
 * @Version		$Id$
 */

require_once 'Auth/Provider/Manager.php';
require_once 'User.php';

class Auth {
	private $_manager	=	null;
	
	public function __construct() {
		$this->_manager	=	new Auth_Provider_Manager();
	}
	
	public function getUser() {
		//$user	=	User::getInstance();
		$user	=	Context::registry('user');
		$user->userID		=	$_SESSION['userID'];
		$user->userName		=	$_SESSION['userName'];
		$user->roleKey 		=	$_SESSION['roleKey'];
		
		return $user;
	}
	
	public function login($info) {
		try {
			if ( $result = $this->_manager->doAuthentication($info) ) {
				$user	=	Context::registry('user');
				$user->userID		=	$_SESSION['userID']		=	$result['user_id'];
				$user->userName		=	$_SESSION['userName']	=	$result['user_name'];
				$user->roleKey 		=	$_SESSION['roleKey']	=	$result['role_key'];
				session_write_close();
				
				return true;
			}
		} catch ( Auth_Provider_Exception $e ) {
			//todo : login again
		}
		Msg::add('Auth failed! May be user not existed!');
		return false;
	}
	
	public function isLogin() {
		if ( !empty($_SESSION['userID']) ) {
			return true;
		} else {
			return false;
		}
	}
	
	public function logout() {
		unset($_SESSION['userID'], $_SESSION['userName'], $_SESSION['roleKey']);
	}
}
?>